
var Cc = Components.classes;
var Ci = Components.interfaces;

function MemAboutUI() {
	this.prefs = Cc["@mozilla.org/preferences-service;1"]
	                               .getService(Ci.nsIPrefService)
	                               .getBranch("extensions.memento.");
	this.prefs.QueryInterface(Ci.nsIPrefBranch2);  
	
}

MemAboutUI.prototype = {
		prefs : null,
		
		getCurrentWindow : function() {
		    // Get current window
		    var wm = Cc["@mozilla.org/appshell/window-mediator;1"]
		                   .getService(Ci.nsIWindowMediator);
		    return wm.getMostRecentWindow("navigator:browser");
		},

	
		onKeyPress : function(e) {
			if ( e.keyCode == KeyEvent.DOM_VK_RETURN ) {
				// equivalent of onchange :(
				tree = e.target;
				if (tree.editingRow != null) {
					val = tree.inputField.value;
					if (val == '') {
						tree.inputField.value = 'new';
					} else if (val != 'new') {
						kids = document.getElementById('timeGateTreeChildren');
						ti = document.createElement('treeitem');
						tr = document.createElement('treerow');
						ti.appendChild(tr)
						tc = document.createElement('treecell');
						tc.setAttribute('label', 'new');
						tr.appendChild(tc);
						kids.appendChild(ti);
					}
				}
			}
		},

		setToPrefs : function() {
			
			easter = this.prefs.getBoolPref('showBubble');
			if (easter) {
				vb = window.document.getElementById('mfox_expt_time_bubble');
				vb.setAttribute('hidden', 'false');
			}
			
			var prefList = ['loadCurrent', 'displaySlider',  'displayPicker', 'displayText', 
			                'hideWhenInactive', 'hideToolbarWhenInactive'];
			for (var i=0, name; name = prefList[i]; i++) {
				var e = window.document.getElementById(name);
				e.checked = this.prefs.getBoolPref(name);
			}
			
			var e2 = window.document.getElementById('enabledIconStyle');
			var pref = this.prefs.getCharPref('enabledIconStyle');
			// set selected item to one with pref
			if (pref == 'glow') {
				e2.selectedIndex = 1;
			} else {
				e2.selectedIndex = 0;
			}

			var bubbleList = ['bubbleYears', 'bubbleMonths', 'bubbleDays', 'bubbleHours',
			                  'bubbleMinutes', 'bubbleSeconds'];
			for (var i=0, name; name=bubbleList[i]; i++) {
				var e = window.document.getElementById(name);
				var val = this.prefs.getIntPref(name);
				if (name == 'bubbleMinutes' || name == 'bubbleSeconds') {
					val = val / 5;
				}
				e.selectedIndex = val;
			}	
			
			tgpref = this.prefs.getCharPref("timeGateList");
			tglist = tgpref.split(' | ');
			tgt = window.document.getElementById('timeGateTree');
			kids = window.document.getElementById('timeGateTreeChildren');
			tgt.addEventListener("keypress", memAbout_handleKeyPress, true);
			
			// now add fields to tree
			for (var i=0, tg; tg = tglist[i]; i++) {
				ti = document.createElement('treeitem');
				tr = document.createElement('treerow');
				ti.appendChild(tr)
				tc = document.createElement('treecell');
				tc.setAttribute('label', tg);
				tr.appendChild(tc);
				kids.appendChild(ti);
			}
			
			if (tglist[tglist.length-1] != 'new') {
				ti = document.createElement('treeitem');
				tr = document.createElement('treerow');
				ti.appendChild(tr)
				tc = document.createElement('treecell');
				tc.setAttribute('label', 'new');
				tr.appendChild(tc);
				kids.appendChild(ti);
			}	
		},

		setBoolPref : function(what) {
			// set id to checked
			this.prefs.setBoolPref(what.id, what.checked);
		},

		
		setIntPref : function(what) {
			this.prefs.setIntPref(what.id, parseInt(what.value));
		}, 
		
		setCharPref : function(what) {
			// set id to checked
			this.prefs.setCharPref(what.id, what.value);
		},

		setMenuPref : function(what) {
			if (what.id.indexOf('bubble') == 0) {
				this.prefs.setIntPref(what.id, parseInt(what.selectedItem.value));
			} else {				
				this.prefs.setCharPref(what.id, what.selectedItem.value);
			}
		},

		timegateUp : function(what) {
			stb = window.document.getElementById("memfox_strings");
			tgt = window.document.getElementById('timeGateTree');
			selct = tgt.view.selection.count;
			if (!selct) {
				 return;
			}
			idx = tgt.currentIndex;
			if (idx == 0) {
				alert(stb.getString(memfox.noUp));
			} else {
				prt = window.document.getElementById('timeGateTreeChildren');
				kid = prt.childNodes[tgt.currentIndex];
				sib = kid.previousSibling;
				prt.removeChild(kid);
				prt.insertBefore(kid, sib);
				tgt.view.selection.clearSelection();
				tgt.view.selection.select(idx-1);
			}
		},

		timegateDown : function(what) {
			stb = window.document.getElementById("memfox_strings");
			tgt = window.document.getElementById('timeGateTree');
			selct = tgt.view.selection.count;
			if (!selct) {
				 return;
			}
			idx = tgt.currentIndex;
			prt = window.document.getElementById('timeGateTreeChildren'); 
			kid = prt.childNodes[tgt.currentIndex];
			sib = kid.nextSibling;
			if (sib == null) {
				alert(stb.getString(memfox.noDown));
			} else {
				prt.removeChild(kid);
				if (sib.nextSibling) {
					prt.insertBefore(kid, sib.nextSibling);
				} else {
					prt.appendChild(kid);
				}
				tgt.view.selection.clearSelection();
				tgt.view.selection.select(idx+1);
			}
		},

		timegateSave : function(what) {
			// step through the kids, and construct ' | ' separated string
			stb = window.document.getElementById("memfox_strings");
			tgt = window.document.getElementById('timeGateTree');
			col = tgt.columns.getFirstColumn();
			try {
				val = tgt.view.getCellText(0, col);
				for (var i = 1; i < tgt.view.rowCount ; i++) {
					val += (" | " + tgt.view.getCellText(i, col)); 
				}
				this.prefs.setCharPref('timeGateList', val);
				// XXX to StringBundle
				alert(stb.getString('memfox.save'));
			} catch(e) {
				alert(stb.getString('memfox.noSave'));
			}
		},

		timegateAdd : function(what) {
			tgt = window.document.getElementById('timeGateTree');
			tcol = tgt.columns.getNamedColumn('TimeGateCol');
			
			kids = window.document.getElementById('timeGateTreeChildren');
			ti = document.createElement('treeitem');
			tr = document.createElement('treerow');
			ti.appendChild(tr)
			tc = document.createElement('treecell');
			tc.setAttribute('label', 'new');
			tr.appendChild(tc);
			kids.appendChild(ti);
			
			tgt.startEditing(kids.childNodes.length-1, tcol);
			
		},
		
		timegateDelete : function(what) {
			stb = window.document.getElementById("memfox_strings");
			tgt = window.document.getElementById('timeGateTree');
			selct = tgt.view.selection.count;
			if (!selct) {
				 return;
			}
			idx = tgt.currentIndex;
			prt = window.document.getElementById('timeGateTreeChildren'); 
			
			// Don't allow deleting last actual timegate
			if (prt.childNodes.length <= 2) {
				alert(stb.getString('memfox.noDel'));
			} else {
				kid = prt.childNodes[tgt.currentIndex];
				prt.removeChild(kid);
			}
		}
}

memAboutUi = new MemAboutUI();

function memAboutUi_handleEvent(elm, name) {
	memAboutUi[name](elm);
}

function memAbout_handleKeyPress(e) {
	return memAboutUi.handleKeyPress(e);	
}

function memAboutUi_setToPrefs() {
	return memAboutUi.setToPrefs();
}

